package com.lzy.srb.core.controller.api;


import com.lzy.common.result.R;
import com.lzy.srb.base.util.JwtUtils;
import com.lzy.srb.core.pojo.entity.BorrowInfo;
import com.lzy.srb.core.service.BorrowInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author lzy
 * @since 2023-02-23
 */
@Api("借款信息")
@RestController
@RequestMapping("/api/core/borrowInfo")
@Slf4j
public class BorrowInfoController {

    @Autowired
    BorrowInfoService borrowerInfoService;

    @ApiOperation("获取借款额度")
    @GetMapping("/auth/getBorrowAmount")
    public R getBorrowerAmount(HttpServletRequest request){

        //下面两行是验证用户token
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        BigDecimal borrowAmount = borrowerInfoService.getBorrowAmount(userId);
        return R.ok().data("borrowAmount",borrowAmount);
    }
    @ApiOperation("提交借款申请")
    @PostMapping("/auth/save")
    public R save(@RequestBody BorrowInfo borrowInfo,
                  HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        borrowerInfoService.saveBorrowInfo(borrowInfo,userId);
        return R.ok().message("提交成功");
    }

    @ApiOperation("获取借款申请审批状态")
    @GetMapping("/auth/getBorrowInfoStatus")
    public R getBorrowStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        Integer status = borrowerInfoService.getStatusByUserId(userId);
        return R.ok().data("borrowInfoStatus",status);
    }

//    @GetMapping("/auth/getBorrr")
//    public R getCorrespond(){
//
//    }

}

